package cn.well.cloud.modular.service.impl;

import cn.well.cloud.base.enums.WXTypeEnums;
import cn.well.cloud.base.wechat.api.impl.AppWXUser;
import cn.well.cloud.modular.mapper.UserInfoMapper;
import cn.well.cloud.modular.model.UserInfo;
import cn.well.cloud.modular.service.IUserInfoService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 微信用户 服务实现类
 * </p>
 *
 * @author jiawei
 * @since 2020-05-27
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    private AppWXUser appWXUser = new AppWXUser();

    @Override
    public void saveUserInfo(String openId, String headUrl, String nickName, WXTypeEnums wxType) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<UserInfo>();
        UserInfo userInfo =new UserInfo();
        userInfo.setOpenId(openId);
        qw.setEntity(userInfo);
        userInfo = baseMapper.selectOne(qw);
        if(userInfo ==null){
            userInfo = new UserInfo();
            userInfo.setOpenId(openId);
            userInfo.setHeadUrl(headUrl);
            userInfo.setUserInfoName(nickName);
            userInfo.setWxType(wxType.getCode());
            baseMapper.insert(userInfo);
        }else{
            userInfo.setHeadUrl(headUrl);
            userInfo.setUserInfoName(nickName);
            userInfo.setWxType(wxType.getCode());
            baseMapper.updateById(userInfo);
        }
    }

    @Override
    public UserInfo updateUnSubscribeUserInfo(String appId, String openId) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<UserInfo>();
        UserInfo userInfo =new UserInfo();
        userInfo.setOpenId(openId);
        qw.setEntity(userInfo);
        userInfo = baseMapper.selectOne(qw);
        if(userInfo !=null){
            userInfo.setSubscribe(0);
            baseMapper.updateById(userInfo);
            return  userInfo;
        }else{
            return  null;
        }
    }

    /**
     * 获取全部用户
     * @param authorizerAccessToken
     * @param nextOpenId
     * @param total
     * @param count
     * @return
     */
    private  List<String> getUserList(String authorizerAccessToken, String nextOpenId,int total, int count){
        List<String> allOpenIdList = new ArrayList<String>();
        while (count < total){
            Object object =  appWXUser.getUserInfoList(authorizerAccessToken,nextOpenId);
            if(object!=null) {
                JSONObject json = JSONObject.parseObject(object.toString());
                 total = json.getIntValue("total");
                 count += json.getIntValue("count");
                 nextOpenId = json.getString("next_openid");
                 JSONObject dataJson= json.getJSONObject("data");
                 JSONArray openIdArray = dataJson.getJSONArray("openid");
                 String openIdStr = JSONObject.toJSONString(openIdArray);
                 List<String> openIdlist = JSONObject.parseArray(openIdStr,String.class);
                 allOpenIdList.addAll(openIdlist);
            }
        }
        return  allOpenIdList;
    }


}
